<template>
  <div class="warpper">
    <div class="toolbar" style="float: right;">
      <el-button type="primary" plain @click="handleAdd" style="margin:0 30px;">Add Module</el-button>
    </div>
    <div class="table">
      <el-table :data="tableData" size="small" stripe border style="width: 100%">
        <el-table-column prop="id" label="Id" width="50" align="center" />
        <el-table-column prop="name" label="Name" width="200" align="center" />
        <el-table-column prop="desc" label="Desc" min-width="200" align="center"/>
        <el-table-column label="Functional permissions" min-width="500" align="center">
          <template v-slot="scope">
            <el-tag v-for="(item, index) in scope.row.permissions" :key="index" style="margin-right:20px">
              {{ item.desc }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="Action" width="250" align="center">
          <template slot-scope="scope">
            <el-button plain @click="edit(scope.row.id)">Edit</el-button>
            <el-button plain type="danger" @click="del(scope.row.id)">Delete</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page">
      <el-pagination v-if="isPaginate" background :current-page="page.current_page" :page-sizes="page.sizes"
        :page-size="page.per_page" layout="prev, pager, next" :total="page.total" @size-change="sizeChange"
        @current-change="currentChange" />
    </div>
    <div class="form">
      <el-dialog :title="title" :visible.sync="dialogFormVisible" :close-on-click-modal="false">
        <el-form v-if="dialogFormVisible" ref="ruleForm" :model="formData" :rules="rules" label-position="right">
          <el-form-item label="name" prop="name">
            <el-input v-model="formData.name" autocomplete="off"
              placeholder="Please enter the module name, which must be in English and must match the RESTful routing name on the backend" />
          </el-form-item>

          <el-form-item label="remark" prop="remark">
            <el-input v-model="formData.desc" autocomplete="off" placeholder="Please enter the module description" />
          </el-form-item>

          <el-form-item label="Granting permissions" prop="name">
            <el-select v-model="formData.permissions" multiple clearable placeholder="Please select permissions"
              style="width: 100%">
              <el-option v-for="(item, index) in permissionList" :key="index" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="cancel">Cancel</el-button>
          <el-button type="primary" @click="savePermission">Save</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
  import CURD from "@/mixin/CURD";
  import {
    permissionList
  } from "@/model/module";

  export default {
    name: "ModuleIndex",
    mixins: [CURD],
    data() {
      return {
        module: "module",
        newTitle: "New feature information",
        editTitle: "Edit functional information",
        permissionList
      };
    },
    async created() {

      console.log(permissionList, "121111")


    },
    methods: {
      handleAdd() {
        this.add();
      },
      async edit(id) {
        const {
          show
        } = require(`@/api/module`);
        const {
          data
        } = await show(id);
        const {
          permissions
        } = data;
        const result = [];
        permissions.forEach((item) => {
          result.push(item.name);
        });
        const formData = {
          id: data.id,
          name: data.name,
          desc: data.name,
          permissions: result
        };
        this.formData = formData;
        console.log(this.formData);
        this.isNew = false;
        this.isEdit = true;
        this.setTitle();
        this.dialogFormVisible = true;
      },
      savePermission() {
        this.save("ruleForm");
      },
      cancel() {
        this.dialogFormVisible = false;
      }
    }
  };
</script>

<style>
  .table,
  .toolbar,
  .page {
    margin: 20px;
  }

  .toolbar {
    margin-top: 10px;
    padding: 5px;
  }

  .table {
    margin-bottom: 10px;
  }

  .page {
    margin-top: 10px;
  }

  .page {
    text-align: center;
  }
</style>
